#include "gopaupau.h"
#include "enemies.h"
#include "bullets.h"
#include "mainlaser.h"
#include "debris.h"

#include <stdio.h>
#include <math.h>

void handlerTurret(void *toBeHandled)
{
	Enemy *ene = (Enemy *) toBeHandled;
	
	if(ene->health<0) ene->currentMode = ENEMY_MODE_VANISHED;
	
	//glMatrixMode(GL_PROJECTION);
	//glPushMatrix();
	glLoadIdentity();

	glColor3f(0.0f,0.0f,1.0f);
	glTranslatef(ene->x,ene->y,0.0f);
	glRotatef(ene->heading,0.0f,0.0f,1.0f);
	glBegin(GL_LINE_STRIP);
	glVertex2d(-15,-15);
	glVertex2d(15, -15);
	glVertex2d(15, 15);
	glVertex2d(-15,15);
	glVertex2d(-15,-15);
	glVertex2d(15, 15);
	glEnd();
	glBegin(GL_LINES);
	glVertex2d(-15,15);
	glVertex2d(15,-15);
	glEnd();
	
	//glPopMatrix();
	//glMatrixMode(GL_MODELVIEW);
	
	
	glLoadIdentity();
	glColor3f(0.0f,0.0f,1.0f);
	glTranslatef(ene->x,ene->y,0.0f);
	glRotatef(ene->gunDirection,0.0f,0.0f,1.0f);
	glBegin(GL_POLYGON);
	glVertex2d(0, -3);
	glVertex2d(20, -3);
	glVertex2d(20, 3);
	glVertex2d(0, 3);
	glEnd();
	
	if(ene->currentMode==ENEMY_MODE_NORMAL)
	{
		
		ene->counter++;
		if(ene->counter>15 && playerAlive)
		{
			ene->counter = 0;
			Bullet *bullet = bulletGetNew();
			bullet->x = ene->x + cos(ene->gunDirection/180 * 3.1459f)*20.0f;
			bullet->y = ene->y + sin(ene->gunDirection/180 * 3.1459f)*20.0f;
			bullet->direction = ene->gunDirection;
			bullet->speed = 2.0f;
		}	
		ene->gunDirection+=1.0f;
		if(ene->gunDirection>360.0f) ene->gunDirection-=360.0f;
		
	}
	else if(ene->currentMode == ENEMY_MODE_TURRET_DIRECTSHOOT)
	{
		ene->counter++;
		ene->gunDirection = atan2((player.y - ene->y), (player.x - ene->x))*(180/3.1459f);
		if(ene->counter>150)
		{
			if(ene->counter % 10==0 && ene->counter < 300 && playerAlive)
			{
				ene->gunDirection += (rand()%100 *0.01f)*3.0f - 1.5f ;		
				//ene->counter=0;
				Bullet *bullet = bulletGetNew();
				bullet->x = ene->x + cos(ene->gunDirection/180 * 3.1459f)*20.0f;
				bullet->y = ene->y + sin(ene->gunDirection/180 * 3.1459f)*20.0f;
				bullet->direction = ene->gunDirection;
				bullet->speed = 2.8f;
				bullet->speed_d = -0.0015f;
			}
			if(ene->counter>300) ene->counter =0;
		}
	}	
	
	Ray *ray = rays_enabled_head;
	while(ray!=NULL)
	{
		if(fabs(ray->x - ene->x)<15)
		{
			float ene_bottom = ene->y + 15;
			if(ray->y_tail > ene_bottom && (ray->y_tail - ray->length) < ene_bottom)
			{
				ray->y_hit = ene_bottom;
				ene->health-=0.5f;
				Debris *debris = debrisGetNew();
				debris->x = ray->x;
				debris->y = ene->y + 15;
				debris->speed = rand()%100 / 100.0f + 0.5;
				debris->direction = rand()%30 + 75;
				debris->ageCounter = 100;
				//debris->rotation = rand()%1000 /100.0f + 2;
				debris->rotation = 0.0f;
				debris->angle = debris->direction + 90.0f;
				//glColor3f(1.0f,(ray->intensity / 10.0f)+0.9f, ray->intensity);
				debris->cr = 1.0f;
				debris->cg = (ray->intensity / 10.0f)+0.9f;
				debris->cb = ray->intensity;
				debris->length = 5 * debris->speed;
			}
		}
		ray = ray->next;
	}
	
}






void handlerStriker(void *toBeHandled)
{
	Enemy *ene = (Enemy *) toBeHandled;
	
	if(ene->health<0) ene->currentMode = ENEMY_MODE_VANISHED;
	
	glLoadIdentity();
	glColor3f(1.0f,0.0f,1.0f);
	glTranslatef(ene->x,ene->y,0.0f);
	glRotatef(ene->heading,0.0f,0.0f,1.0f);
	glBegin(GL_LINE_STRIP);
	glVertex2d(10,0);
	glVertex2d(-5, 5);
	glVertex2d(-5, -5);
	glVertex2d(10,0);
	glEnd();
}

